package com.weirong.smartvault.backservice.controller;

import com.weirong.smartvault.backservice.controller.fragment_entity.PagesIndex;
import com.weirong.smartvault.dao.EquipmentErrorLogRepository;
import com.weirong.smartvault.entity.EquipmentErrorLog;
import com.weirong.smartvault.backservice.service.EquipmentErrorLogService;
import com.weirong.smartvault.backservice.util.common.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.util.*;

/**
 * 于2017/11/30由 KFEB4 创建 .
 */
@Controller
public class monitor {
    @Autowired
    private EquipmentErrorLogRepository equipmentErrorLogRepository;

    @Autowired
    private EquipmentErrorLogService equipmentErrorLogService;

    @RequestMapping(value = "/monitor/error_log", method = RequestMethod.GET)
    public String errorLog(ModelMap model, @RequestParam(value = "page", defaultValue = "0") Integer page,
                           @RequestParam(value = "size", defaultValue = "10") Integer size, @RequestParam(value = "start", defaultValue = "") String s,
                           @RequestParam(value = "end", defaultValue = "") String e) throws ParseException {
        Date start = DateUtil.getTodayOrParse(s);
        Date end = DateUtil.getTodayOrParse(e);
        if (e.length() == 0) {
            end = DateUtil.getDateNext(start);
        }
        Page<EquipmentErrorLog> deliveryLogs = equipmentErrorLogService.findAllByCreateTimeBetweenWithPageing(start, end, page, size);
        model.addAttribute("deliveryLogs", deliveryLogs);

        model.addAttribute("PagesIndex", new PagesIndex(page, deliveryLogs.getTotalPages(),"start",s,"end",e));

        return "/monitor/error_log";
    }
}
